package com.etime.shycourse.controller.wx.applets.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.config.Tools;
import com.etime.shycourse.dto.CoursecommentDto;
import com.etime.shycourse.dto.CurrentUser;
import com.etime.shycourse.pojo.Coursecomment;
import com.etime.shycourse.services.interfaces.WxCoursecommentServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wb
 * @since 2022-03-02
 */
@Api(tags = "微信课程评价管理数据接口")
@RestController
@RequestMapping("/wx/courseComment")
public class WxCoursecommentController {
    @Autowired
    private WxCoursecommentServices services;
    @Autowired
    private Tools tools;

    @ApiOperation("课程评论条数接口")
    @ApiImplicitParam("根据课程id 统计评论条数")
    @GetMapping("/course/count/{CourseId}")
    public BaseMessage countComments(@PathVariable Integer CourseId) {
        int rtn = services.countComment (CourseId);
        BaseMessage msg = new BaseMessage ();
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("统计该课程评论条数成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("统计该课程评论条数失败");
        }
        return msg;
    }

    @ApiOperation("课程提问条数接口")
    @ApiImplicitParam("根据课程id 统计提问条数")
    @GetMapping("/questions/count/{courseId}")
    public BaseMessage countQuestions(@PathVariable Integer courseId) {
        int rtn = services.countQuestions (courseId);
        BaseMessage msg = new BaseMessage ();
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("统计该课程提问条数成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("统计该课程提问条数失败");
        }
        return msg;
    }


    @ApiOperation("创建课程的评论或者提问")
    @PostMapping("/insert")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseid", value = "必传 课程id"),
            @ApiImplicitParam(name = "commentcontent", value = "必传评论内容"),
            @ApiImplicitParam(name = "commenttype", value = "提问 1--评论 2--提问 可根据需要传值"),
            @ApiImplicitParam(name = "commentisshow", value = "评论是否展示（必选）。1展示，2不展示")
    })
    public PojoMessage<Coursecomment> insetCourseComment(@RequestBody Coursecomment coursecomment, HttpServletRequest request) {
        PojoMessage<Coursecomment> msg = new PojoMessage ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息,可能登录超时");
            return msg;
        }
        int integer = services.insetCourseComment (coursecomment, token);
        //System.out.println(coursecomment);
        if (integer == 0) {
            msg.setCode (0);
            msg.setMsg ("评论失败");
        } else if (integer == -1) {
            msg.setCode (-1);
            msg.setMsg ("系统繁忙，请稍后重新进行评论");
        } else if (integer == -3) {
            msg.setCode (-3);
            msg.setMsg ("您评论的内容中包含敏感词汇,请修改后再试！");
        } else if (integer == 1) {
            msg.setCode (integer);
            msg.setMsg ("评论成功");
            msg.setData (coursecomment);
        }
        return msg;
    }

//    @ApiOperation("对已经插入评论 进行评价(回复) 评论：1 默认   commenttype= 2--->提问、 commenttype= 1 -->评论")
//    @PostMapping("/inserts")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "courseid", value = "必传 课程id"),
//            @ApiImplicitParam(name = "commentcontent", value = "必传 评论内容"),
//            @ApiImplicitParam(name = "commenttype", value = "评论：1 默认     提问 1--评论 2--提问 可根据需要传值"),
//    })
//    public BaseMessage insetCourseComments(Coursecomment coursecomment, HttpServletRequest request) {
//        BaseMessage msg = new BaseMessage ();
//        String token = request.getHeader ("token");
//        if (token == null || token.equals ("")) {
//            msg.setCode (-2);
//            msg.setMsg ("未获取到用户信息；可能登录超时");
//            return msg;
//        }
//        int integer = services.insetCourseComments (coursecomment, token);
//        if (integer == 0) {
//            msg.setCode (0);
//            msg.setMsg ("评论失败");
//        } else if (integer == -1) {
//            msg.setCode (-1);
//            msg.setMsg ("系统繁忙，此时请开发者稍候再试");
//        } else if (integer == -2) {
//            msg.setCode (-2);
//            msg.setMsg ("您回复的内容用词不当,请修改后再试！");
//        } else if (integer == 1) {
//            msg.setCode (integer);
//            msg.setMsg ("评论成功");
//        }
//        return msg;
//    }

//    @ApiOperation("根据课程id 查询课程的评论，最多10条评论")
//    @GetMapping("/get/{CourseId}")
//    public ListMessage<Coursecomment> selectCommentList(@PathVariable Integer CourseId, HttpServletRequest request) {
//        ListMessage<Coursecomment> msg = new ListMessage<> ();
//        List<Coursecomment> data = services.selectCommentList (CourseId);
//        if (data != null) {
//            msg.setCode (1);
//            msg.setDatalist (data);
//        } else {
//            msg.setCode (0);
//            msg.setMsg ("没有找到课时信息");
//        }
//        return msg;
//    }

    @ApiOperation("根据课程id分页查询评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseid", value = "课程id"),
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行")
    })
    @GetMapping("/getpagebyid")
    public PageMessage<CoursecommentDto> selectCommontByPage(Integer courseid, Long pagenum, Long pagesize) {
        Page<CoursecommentDto> page = services.selectCommentByPage (courseid, pagenum, pagesize);
        PageMessage<CoursecommentDto> msg = new PageMessage<> ();
        if (page != null) {
            msg.setCode (1);
            msg.setPagedata (page);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("根据评论id 查询对评论的回复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "CommentID", value = "评论id"),
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行")
    })
    @GetMapping("/get/recomment")
    public PageMessage<Coursecomment> selectCommentListById(Integer CommentID, Long pagenum, Long pagesize) {
        PageMessage<Coursecomment> msg = new PageMessage<> ();
        Page<Coursecomment> data = services.selectReCommentByPage (CommentID, pagenum, pagesize);
        if (data != null) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到课时信息");
        }
        return msg;
    }

    @ApiOperation("根据评论id查询评论详细信息，会查询出评论课程的名称。" +
            "<br>用于在回复评论页面中展示该条评论的详情")
    @ApiImplicitParam(name = "commentid", value = "评论id")
    @GetMapping("/getcomment/byid/{commentid}")
    public PojoMessage<CoursecommentDto> selectCommentById(@PathVariable("commentid") Integer commentid) {
        PojoMessage<CoursecommentDto> msg = new PojoMessage<> ();
        CoursecommentDto comment = services.selectCommentById (commentid);
        if (comment != null) {
            msg.setCode (1);
            msg.setData (comment);
        } else {
            msg.setCode (0);
            msg.setMsg ("查询评论信息失败，请稍后再试");
        }
        return msg;
    }

    @ApiOperation("根据用户id查询用户的提问或者评论列表；<br>用于普通用户的我的提问/评论查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commenttype", value = "1:评论；2：提问"),
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行")
    })
    @GetMapping("/get/usercomment")
    public PageMessage<CoursecommentDto> selectMyCommentByUserid(Integer commenttype, Long pagenum, Long pagesize, HttpServletRequest request) {
        PageMessage<CoursecommentDto> msg = new PageMessage<> ();
        String token = request.getHeader ("token");
        CurrentUser currentUser = tools.getCurrentUserFromRedis (token);
        if (currentUser == null) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
        } else {
            Page<CoursecommentDto> data = services.selectCommentPageByUserId (currentUser.getUser ().getUserid (), commenttype, pagenum, pagesize);
            if (data != null) {
                msg.setCode (1);
                msg.setPagedata (data);
            } else {
                msg.setCode (0);
                msg.setMsg ("未查询到数据");
            }
        }
        return msg;
    }

    @ApiOperation("根据教师id查询教师所有课程的提问或者评论列表；<br>用于教师用户的我的提问/评论查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commenttype", value = "1:评论；2：提问"),
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行")
    })
    @GetMapping("/get/teachercomment")
    public PageMessage<CoursecommentDto> selectMyCommentByTeacherId(Integer commenttype, Long pagenum, Long pagesize, HttpServletRequest request) {
        PageMessage<CoursecommentDto> msg = new PageMessage<> ();
        String token = request.getHeader ("token");
        CurrentUser currentUser = tools.getCurrentUserFromRedis (token);
        if (currentUser == null) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
        } else {
            if (currentUser.getTecid () == null || currentUser.getTecid () == 0) {
                //教师id不存在，表示还不是教师
                msg.setCode (-1);
                msg.setMsg ("您还不是教师");
            } else {
                Page<CoursecommentDto> data = services.selectCommentPageByTeacherId (currentUser.getTecid (), commenttype, pagenum, pagesize);
                if (data != null) {
                    msg.setCode (1);
                    msg.setPagedata (data);
                } else {
                    msg.setCode (0);
                    msg.setMsg ("未查询到数据");
                }
            }
        }
        return msg;
    }

    @ApiOperation("根据教师id查询教师还有多少未回复的提问。用于提示教师进行回复")
    @GetMapping("/get/notrecomment/count")
    public BaseMessage selectNotReCommentByTeacherId(HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        CurrentUser currentUser = tools.getCurrentUserFromRedis (token);
        if (currentUser == null) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
        } else {
            if (currentUser.getTecid () == null || currentUser.getTecid () == 0) {
                //教师id不存在，表示还不是教师
                msg.setCode (-1);
                msg.setMsg ("您还不是教师");
            } else {
                int count = services.selectNotReCommentByTeacherId (currentUser.getTecid ());
                msg.setCode (count);
                msg.setMsg (count + "");
            }
        }
        return msg;
    }

    //    @ApiOperation("根据用户token 查询我的提问列表")
//    @GetMapping("/my/questions/get")
//    public ListMessage<CoursecommentDto> selectMyQuestionsCommentList(HttpServletRequest request) {
//        ListMessage<CoursecommentDto> msg = new ListMessage<> ();
//        String token = request.getHeader ("token");
//        if (token == null || token.equals ("")) {
//            msg.setCode (-2);
//            msg.setMsg ("未获取到用户信息；可能登录超时");
//            return msg;
//        }
//        List<CoursecommentDto> data = services.selectMyQuestionsCommentList (token);
//        if (data != null) {
//            msg.setCode (1);
//            msg.setDatalist (data);
//        } else {
//            msg.setCode (0);
//            msg.setMsg ("没有找到课时信息");
//        }
//        return msg;
//    }
//
//    @ApiOperation("根据用户token 查询我的评论列表")
//    @GetMapping("/my/comment/get")
//    public ListMessage<CoursecommentDto> selectMyCommentList(HttpServletRequest request) {
//        ListMessage<CoursecommentDto> msg = new ListMessage<> ();
//        String token = request.getHeader ("token");
//        if (token == null || token.equals ("")) {
//            msg.setCode (-2);
//            msg.setMsg ("未获取到用户信息；可能登录超时");
//            return msg;
//        }
//        List<CoursecommentDto> data = services.selectMyCommentList (token);
//        if (data != null) {
//            msg.setCode (1);
//            msg.setDatalist (data);
//        } else {
//            msg.setCode (0);
//            msg.setMsg ("没有找到课时信息");
//        }
//        return msg;
//    }
    @ApiOperation("微信课程评论信息接口")
    @ApiImplicitParam("据课程评论的id 进行修改 评论的置顶，加精")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentid", value = "必传值 评论的id"),
            @ApiImplicitParam("没有中文注释的，根据前端的需要传值"),
    })
    @PostMapping("/update")
    public BaseMessage updateCommentByIds(@RequestBody Coursecomment coursecomment) {
        BaseMessage msg = new BaseMessage ();
        if (coursecomment == null) {
            msg.setCode (-2);
            msg.setMsg ("修改课程为空");
        }
        Integer rtn = services.updateCommentById (coursecomment);
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("修改课程评论成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改课程评论失败");
        }
        return msg;
    }
}

